package com.itheima.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.common.threadlocal.UserHolder;
import com.itheima.common.util.BeanHelper;
import com.itheima.sys.dto.SysFunctionDTO;
import com.itheima.sys.entity.SysFunction;
import com.itheima.sys.mapper.SysFunctionMapper;
import com.itheima.sys.service.ISysFunctionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 系统功能表 系统-系统功能表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2020-12-13
 */
@Service
public class SysFunctionServiceImpl extends ServiceImpl<SysFunctionMapper, SysFunction> implements ISysFunctionService {

    @Override
    public List<SysFunctionDTO> queryFunctions(Long companyUserId) {
        List<SysFunction> functionList = this.getBaseMapper().queryFunctionsByCompanyUserId(companyUserId);
        if(!functionList.isEmpty()){
            return BeanHelper.copyWithCollection(functionList, SysFunctionDTO.class);
        }
        return null;
    }

    @Override
    public List<SysFunction> queryFunctionsByRoleId(Long roleId, Long companyId) {
        return this.getBaseMapper().queryFunctionsByRoleId(roleId, companyId);
    }

    @Override
    public List<SysFunctionDTO> queryFunctionsByCompanyId() {
        Long companyId = UserHolder.getCompanyId();
        QueryWrapper<SysFunction> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysFunction::getCompanyId, companyId);
        List<SysFunction> sysFunctionList = this.list(queryWrapper);
        if(!sysFunctionList.isEmpty()){
            return BeanHelper.copyWithCollection(sysFunctionList, SysFunctionDTO.class);
        }
        return null;
    }
}
